/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.Controller;

import DTO.Store;
import Exception.KwickKioskDBException;
import admin.BusinessLogic.BusinessLogic;
import admin_gui.Admin_EditStore;
import admin_gui.Admin_SearchStore;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author WEN
 */
public class StoreController {

    private BusinessLogic businesslogic;
    private JPanel mainPanel;
    private Vector store;
    private Vector wholevector;
    private boolean[] storeItem;
    private int deleteType = 0;             //to remember the selection type before deleting
    private String searchSelection = "";

    public StoreController(JPanel MainPanel) {
        try {
            //  System.out.println("in store controller1");
            businesslogic = new BusinessLogic();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StoreController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StoreController.class.getName()).log(Level.SEVERE, null, ex);
        }
        mainPanel = MainPanel;
        store = new Vector();
        storeItem = new boolean[6];
        storeItem[0] = true;
        for (int i = 1; i < 6; i++) {
            storeItem[i] = false;
        }
    }

    public boolean addStore(String storeName, String phone, String taxCode,
            String aptNo, int stNo, String streetName, String postCode,
            String city, String province) {
        boolean bol = false;
        bol = businesslogic.addStore(storeName, phone, taxCode, aptNo, stNo, streetName, postCode, city, province);

        return bol;

    }

    public void manageStore(JTable ResultTable, Vector v_head,
            String searchField, boolean[] storeItem) {
        this.storeItem = storeItem;
        if (storeItem[0] == true || searchField.length() == 0) {
            showStoreTable(ResultTable, v_head);
        } else if (storeItem[1] == true) {
            showStoreTableByName(searchField.trim(), ResultTable, v_head);
            deleteType = 1;
            searchSelection = searchField.trim();
        } else if (storeItem[2] == true) {
            showStoreTableByPhone(searchField.trim(), ResultTable, v_head);
            deleteType = 2;
            searchSelection = searchField.trim();
        } else if (storeItem[3] == true) {
            showStoreTableByPostcode(searchField.trim(), ResultTable, v_head);
            deleteType = 3;
            searchSelection = searchField.trim();
        } else if (storeItem[4] == true) {
            showStoreTableByCity(searchField.trim(), ResultTable, v_head);
            deleteType = 4;
            searchSelection = searchField.trim();
        } else if (storeItem[5] == true) {
            showStoreTableByProvince(searchField.trim(), ResultTable, v_head);
            deleteType = 5;
            searchSelection = searchField.trim();
        } else {
            JOptionPane.showMessageDialog(null,
                    "Sorry! Can not find that information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showStoreTable(JTable ResultTable, Vector v_head) {
     //   System.out.println("vector:" + store);
        store = businesslogic.getStore();
        DefaultTableModel tm = new DefaultTableModel(store, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showStoreTableByName(String storename, JTable ResultTable,
            Vector v_head) {
        store.clear();
        store = businesslogic.getStoresByStoreName(storename);
        DefaultTableModel tm = new DefaultTableModel(store, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showStoreTableByPhone(String phonenumber, JTable ResultTable,
            Vector v_head) {
        store.clear();
        store = businesslogic.getStoresByPhonenumber(phonenumber);
        DefaultTableModel tm = new DefaultTableModel(store, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showStoreTableByPostcode(String postcode, JTable ResultTable,
            Vector v_head) {
        store.clear();
        store = businesslogic.getStoresByPostcode(postcode);
        DefaultTableModel tm = new DefaultTableModel(store, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showStoreTableByCity(String city, JTable ResultTable,
            Vector v_head) {
        store.clear();
        store = businesslogic.getStoresByCity(city);
        DefaultTableModel tm = new DefaultTableModel(store, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showStoreTableByProvince(String province, JTable ResultTable,
            Vector v_head) {
        store.clear();
        store = businesslogic.getStoresByProvince(province);
        DefaultTableModel tm = new DefaultTableModel(store, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void showEditStorePanel(String storeID) {
        wholevector = businesslogic.getWholeStoresByStoreName(storeID);
        Admin_EditStore editStorePane = new Admin_EditStore(this, wholevector);
        editStorePane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(editStorePane);
        mainPanel.validate();
        mainPanel.repaint();
    }

    public void updateStore(Store store) {
     //   System.out.println("inside updateStore");
        if (businesslogic.updateStore(store)) {
            JOptionPane.showMessageDialog(null, "The store information has been updated!",
                    "Message", JOptionPane.INFORMATION_MESSAGE);
            Admin_SearchStore searchStorePane = new Admin_SearchStore(mainPanel);
            searchStorePane.setBounds(1, 1, 762, 613);
            mainPanel.removeAll();
            mainPanel.add(searchStorePane);
            mainPanel.validate();
            mainPanel.repaint();

        }
    }

    public void deleteStore(String storeName, JTable ResultTable, Vector v_head) {
        if (businesslogic.deleteStore(storeName)) {
     //       System.out.println("successfully deleting!");
            if (deleteType == 0) {
                showStoreTable(ResultTable, v_head);
            } else if (deleteType == 1) {
                showStoreTableByName(searchSelection, ResultTable, v_head);
            } else if (deleteType == 2) {
                showStoreTableByPhone(searchSelection, ResultTable, v_head);
            } else if (deleteType == 3) {
                showStoreTableByPostcode(searchSelection, ResultTable, v_head);
            } else if (deleteType == 4) {
                showStoreTableByCity(searchSelection, ResultTable, v_head);
            } else {
                showStoreTableByProvince(searchSelection, ResultTable, v_head);
            }
        }
    }

    public void goBackSearchStore() {
        Admin_SearchStore searchStorePane = new Admin_SearchStore(mainPanel);
        searchStorePane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(searchStorePane);
        mainPanel.validate();
        mainPanel.repaint();
    }
}
